package com.superme.ResConsumerServiceByribbon.service;


import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.security.oauth2.client.OAuth2RestTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;

/**
 * Created by Administrator on 2017/7/4.
 */
@Service
public class TestService {
    private static final Logger LOGGER = LoggerFactory.getLogger(TestService.class);
    @Autowired
    private LoadBalancerClient loadBalancerClient;

    @Autowired
    private  OAuth2RestTemplate rest;

    @Autowired
    private DiscoveryClient client;

    //@Autowired
    //OAuth2ClientContext oauth2Context;

/*
    @HystrixCommand(fallbackMethod = "testFallback",commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value="5000"),
            @HystrixProperty(name="metrics.rollingStats.timeInMilliseconds",value="10000"),
            @HystrixProperty(name="execution.isolation.strategy",value="SEMAPHORE")
    },threadPoolProperties = {
            @HystrixProperty(name="coreSize",value="1"),
            @HystrixProperty(name="maxQueueSize",value="0")
    })
*/
    @HystrixCommand(fallbackMethod = "testFallback")
    public String test() {
        // ClientCredentialsResourceDetails ccr =  new ClientCredentialsResourceDetails();
        //OAuth2RestTemplate rest = OAuth2RestTemplate(ccr,oauth2Context);

        //ServiceInstance si = this.client.getLocalServiceInstance();
        //String log = String.format("test,host:%s,service_id:%s",si.getHost(),si.getServiceId());
        TestService.LOGGER.info(GetChooseLog());
        String url = "http://RES-SERVICE/";
        //如果用这个链接的话此服务必须是通过zuul调用，或者 dummy-service有自己的认证机制
        //否则只能通过http://gatewayserver:8765/dummy/进行调用
        HttpHeaders headers = new HttpHeaders();
        HttpEntity entity =  new HttpEntity<String>(headers);
        HashMap<String,String> urivarmaps = new HashMap<String,String>();
        ResponseEntity<String> response = rest.exchange(url, HttpMethod.GET,entity, String.class, urivarmaps);
        return  response.getBody();

    }
    public String testFallback() {
        //ServiceInstance si = this.loadBalancerClient.choose("DUMMY-SERVICE");
        //String str =  "can not use http://DUMMY-SERVICE/,please re try!"+ si.getServiceId()+":"+si.getHost()+":"+si.getPort();
        //ResourcesHomeController.LOGGER.info(str);
        TestService.LOGGER.info(GetChooseLog());
        String str =  "can not use http://RES-SERVICE/,please re try!";
        TestService.LOGGER.info(str);
        return str;

    }

    public String GetChooseLog()
    {
        ServiceInstance si = this.loadBalancerClient.choose("RES-SERVICE");
        String Log = si.getServiceId()+":"+si.getHost()+":"+si.getPort();
        return Log;
    }

}
